
** South Africa
** Clean Wave 4

*************
*************

	** ROSTER
		use "$dir/rawdata/NIDS South Africa/wave5/HouseholdRoster_W5_Anon_V1.0.0.dta", clear
		gen w5_inhh=w5_r_pres==1 if !missing(w5_r_pres)
					
	** Household size
		gen temp=1 if w5_inhh==1
		bys w5_hhid: egen w5_hh_size=total(temp)
		drop temp
	
	** Marital status
		gen w5_married=w5_r_mar==1 if !missing(w5_r_mar)
		replace w5_married=0 if w5_r_noparh==3 | w5_r_noparh==4 | w5_r_noparh==5
		
	** Save
		keep w5_hhid pid w5_inhh w5_hh_size w5_married
		tempfile roster
		save `roster', replace
		
*************
*************

	** INDIVIDUAL FILES (DERIVED)
		use "$dir/rawdata/NIDS South Africa/wave5/indderived_W5_Anon_V1.0.0.dta", clear
		
	** Female
		gen w5_female=w5_best_gen==2 if !missing(w5_best_gen) & w5_best_gen>0
		
	** Age
		gen w5_age=w5_best_age_yrs
		
	** Works
		gen w5_work_any_c=w5_empl_stat==3 if !missing(w5_empl_stat)
		
		keep w5_hhid pid w5_age w5_work_any_c w5_female
		tempfile indderiv
		save `indderiv', replace
			 
	** INDIVIDUAL FILES
		use "$dir/rawdata/NIDS South Africa/wave5/Adult_W5_Anon_V1.0.0.dta", clear
		
	** Employment
		gen w5_work_primaryocc=w5_a_em1==1 if !missing(w5_a_em1) & w5_a_em1>0
		gen w5_work_secondaryocc=w5_a_em2==1 if !missing(w5_a_em2) & w5_a_em2>0
		gen w5_work_selfemp=w5_a_ems==1 if !missing(w5_a_ems) & w5_a_ems>0
		gen w5_work_casual=w5_a_emc==1 if !missing(w5_a_emc) & w5_a_emc>0
		gen w5_work_agri=w5_a_emp==1 if !missing(w5_a_emp) & w5_a_emp>0
		gen w5_work_bizhelp=w5_a_emh==1 if !missing(w5_a_emh) & w5_a_emh>0
		
		egen w5_work_any=rowmax(w5_work_primaryocc w5_work_secondaryocc w5_work_selfemp w5_work_casual w5_work_agri w5_work_bizhelp)

	** Work hours
		foreach var in w5_a_em1hrs w5_a_em2hrs w5_a_emshrs w5_a_emchrs w5_a_emphrs w5_a_emhhrs {
			replace `var'=. if `var'<0
		}
		egen w5_work_hrs=rowtotal(w5_a_em1hrs w5_a_em2hrs w5_a_emshrs w5_a_emchrs w5_a_emphrs w5_a_emhhrs)
	
	** CESD
		foreach var in w5_a_emobth w5_a_emomnd w5_a_emodep w5_a_emoeff w5_a_emohope w5_a_emofear w5_a_emoslp w5_a_emohap w5_a_emolone w5_a_emogo {
			recode `var' -8=. -3=. -9=.
			recode `var' 1=0 2=1 3=2 4=3
		}
		replace w5_a_emohope=3-w5_a_emohope
		replace w5_a_emohap=3-w5_a_emohap
		egen w5_cesd_total=rowtotal(w5_a_emobth w5_a_emomnd w5_a_emodep w5_a_emoeff w5_a_emohope w5_a_emofear w5_a_emoslp w5_a_emohap w5_a_emolone w5_a_emogo)
		egen w5_cesd_count=rownonmiss(w5_a_emobth w5_a_emomnd w5_a_emodep w5_a_emoeff w5_a_emohope w5_a_emofear w5_a_emoslp w5_a_emohap w5_a_emolone w5_a_emogo)
		replace w5_cesd_total=. if w5_cesd_count<8 // don't score if more than 2 are missing
		
		gen w5_depressed=w5_cesd_total>=10

	** DISABILITIES
		tab1 w5_a_hlvisaid w5_a_hlhraid 
		tab1 w5_a_hlvisaid w5_a_hlhraid, nol

	** Difficulty seeing
		gen w5_diff_seei = w5_a_hlvisaid
		replace w5_diff_seei = 0 if w5_a_hlvisaid==2
		replace w5_diff_seei = . if w5_a_hlvisaid < 0
		tab1 w5_diff_seei

	** Difficulty hearing
		gen w5_diff_hear = w5_a_hlhraid
		replace w5_diff_hear = 0 if w5_a_hlhraid==2
		replace w5_diff_hear = . if w5_a_hlhraid < 0
		tab1 w5_diff_hear

	** Difficulty walking or climbing stairs

		*na


	** Difficulty remembering or concentrating
		*na

	** Difficulty with self-care

		*na

	** Difficulty communicating

		*na

		keep w5_hhid pid w5_work_any w5_work_hrs w5_work_primaryocc w5_work_secondaryocc w5_work_selfemp w5_work_casual w5_work_agri w5_work_bizhelp w5_cesd_count w5_depressed w5_diff_seei w5_diff_hear
		tempfile indiv
		save `indiv', replace
		
*************
*************

	** HOUSEHOLD DATA 

		use "$dir/rawdata/NIDS South Africa/wave5/hhderived_W5_Anon_V1.0.0.dta", clear
		
		gen w5_urban=w5_geo2011==2 if !missing(w5_geo2011)
		gen w5_hh_size_check=w5_hhsizer
		keep w5_hhid w5_hh_size_check w5_urban
			
		tempfile hh
		save `hh', replace
		
*************
*************

	** APPEND
		
		use `hh', clear
		merge 1:m w5_hhid using `roster', assert(matched) nogen
		merge 1:1 w5_hhid pid using `indderiv', assert(master matched) gen(merge_indderiv)
		merge 1:1 w5_hhid pid using `indiv', assert(master matched) gen(merge_indiv)
		
		order w5_hhid pid
		drop merge*
		
		compare w5_work_any_c w5_work_any
		drop w5_work_any_c
		compare w5_hh_size_check w5_hh_size
		drop w5_hh_size_check
		
		save "$dir/data/saf_w5_individual.dta", replace
		
	** Missing variables: 
	** Urban status
	
	** Illness and injury

	** Impact of illness and injury
		